package com.platform.car.controller;

import com.platform.common.config.Global;
import com.platform.common.page.Page;
import com.platform.car.model.City;
import com.platform.car.model.Parking;
import com.platform.car.service.CityService;
import com.platform.car.service.ParkingService;
import com.platform.common.utils.UUIDFactory;
import com.platform.common.utils.UserUtils;
import com.platform.upms.model.UpmsPicture;
import com.platform.upms.model.UpmsUser;
import com.platform.upms.model.json.Tip;
import com.platform.upms.service.UpmsPictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/car/parking")
public class ParkingController {

    @Autowired
     ParkingService parkingService;

    @Autowired
     CityService cityService;

    @Autowired
    UpmsPictureService upmsPictureService;


    /**
     * 带分页的查询列表
     * @param page
     * @param modelMap
     * @return
     */
    @RequestMapping("/list")
    public String list(Page<Parking> page, ModelMap modelMap,Parking parking){
        if(!UserUtils.isHighestPower()) {
            parking.setOrgId(UserUtils.getUserOrgId());
        }
        Page<Parking> parkingList = parkingService.findByPage(page,parking);
        modelMap.addAttribute("parkingList",parkingList);
        return  "/car/parking/list.jsp";
    }

    /**
     * 添加页面
     * @return
     */
    @RequestMapping("/add")
    public String add(ModelMap modelMap,HttpSession session){
        List<City> provinces= cityService.chooseNext(1);
        modelMap.put("provinces",provinces);
        return "/car/parking/add.jsp";
    }

    /**
     * 保存数据
     * @param parking
     * @return
     */
    @RequestMapping("/saveData")
    @ResponseBody
    public Tip saveData(Parking parking,HttpSession session){
        try{
            UpmsUser upmsUser= (UpmsUser) session.getAttribute(Global.UPMSUSER);
            if(upmsUser.getOrgId() !=null){
                parking.setOrgId(upmsUser.getOrgId());
            }
            parking.setCreateTime(new Date());
            parkingService.saveData(parking);
            return  new Tip("保存成功");
        }catch (Exception e){
            e.printStackTrace();
            return  new Tip(1,"保存失败");
        }
    }

    /**
     * 修改页面
     * @param id
     * @return
     */
    @RequestMapping("/edit")
    public String edit(String id,ModelMap modelMap){
        Parking parking = parkingService.get(id);
        showProvinces(modelMap,id);//获取到省市区
        modelMap.addAttribute("parking",parking);
        UpmsPicture upmsPicture=null;
        if(parking.getImgId() !=null){
             upmsPicture = upmsPictureService.get(parking.getImgId());
        }
       modelMap.put("picture",upmsPicture);
        return "/car/parking/edit.jsp";
    }

    /**
     * 获取到省市区
     * @param modelMap
     * @param id
     */
    private void showProvinces(ModelMap modelMap, String id){
        List<City> cities = cityService.chooseNext(1);
        modelMap.addAttribute("provinces",cities);
        Parking parking =parkingService.get(id);
        modelMap.put("city", cityService.chooseNext(cityService.selectByName(parking.getProvince()).getId()));
        modelMap.put("county", cityService.chooseNext(cityService.selectByName(parking.getCity()).getId()));
    }

    /**
     * 修改数据
     * @param parking
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Tip update(Parking parking){
        try{
            parkingService.update(parking);
            return  new Tip("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return  new Tip(1,"修改失败");
        }
    }

    /**
     * 删除数据
     * @param ids
     * @return
     */
    @RequestMapping("/deleteDataByIds")
    @ResponseBody
    public Tip deleteDataByIds(String ids[]){
        try {
            parkingService.deleteDataByIds(ids);
            return  new Tip("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return  new Tip(1,"删除失败");
        }
    }

    /**
     * 根据ID查询停车场
     * @param parkingId
     * @return
     */
    @RequestMapping("/selectById")
    @ResponseBody
    public Parking selectById(String parkingId){
        Parking parking = parkingService.get(parkingId);
        return  parking;
    }



}
